package com.company.controller;

import com.company.model.UserModel;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("执行了后台的servlet业务逻辑。。。。。。。。。。。");
        /**
         *要求request.getParameter方法的参数名于form表单控件的name属性值保持一致
         */
        //获取表单数据
        String strName = request.getParameter("userName");
        String strPsw = request.getParameter("userPsw");
        System.out.println("用户名："+strName);
        System.out.println("密码："+strPsw);

        //跳转到业务逻辑成功的页面
        //业务逻辑处理对象
        UserModel userModel = new UserModel();
        boolean canLogined = userModel.doLogin(strName,strPsw);
        if (canLogined){
            //登录成功
            //转发
            request.getRequestDispatcher("WEB-INF/viewpages/loginedmain.jsp").forward(request,response);
        }else {
            //失败之后重定向到index页面
            //重定向
            response.sendRedirect("index.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //post 请求才用get获取登录数据
        doGet(request,response);
    }
}
